package com.example.module.spinner;

import android.graphics.Camera;
import android.graphics.Matrix;
import android.util.Log;
import android.view.animation.Animation;
import android.view.animation.Transformation;

public class SpinAnimation extends Animation {
	private final float fromXScale = 1.0f;
	private final float toXScale = 0.1f;
	private final float fromYScale = 1.0f;
	private final float toYScale = 0.1f;
	private final float fromDegrees = 0.0f;
	private final float toDegrees = 360.0f;
	
	private int width;
	private int height;
	
	
//	private final float mCenterX;
//	private final float mCenterY;
	private Camera mCamera;


	@Override
	public void initialize(int width, int height, int parentWidth, int parentHeight) {
		this.width = width;
		this.height = height;
		super.initialize(width, height, parentWidth, parentHeight);
		mCamera = new Camera();
	}

	/**
	 * <scale 
	android:fromXScale="1.0"
      android:toXScale="0.1"
      android:fromYScale="1.0"
      android:toYScale="0.1"
      android:duration="2000"
      
      android:pivotX="0"
      android:pivotY="100%"
   />
   <rotate 
	android:fromDegrees="0.0"
      android:toDegrees="360"
      android:duration="2000"
      
      android:pivotX="50%"
      android:pivotY="50%"
      
   />
	 */
	
	/**		XScale : 1.0 ~ 0.1 => 100 ~ 10
	 * 		YScale : 1.0 ~ 0.1
	 * 		interpolatedTime : 0.0 ~ 1.0 
	 */
	@Override
	protected void applyTransformation(float interpolatedTime, Transformation t) {
		final float fromDegrees = this.fromDegrees;
		final float xScale = fromXScale + ((toXScale - fromXScale) * interpolatedTime);
		final float yScale = fromYScale + ((toYScale - fromYScale) * interpolatedTime);
		final float degrees = fromDegrees + ((toDegrees - fromDegrees) * interpolatedTime);
		
		Log.e("applyTransformation", "width=" + width + " height= " + height + " xScale=" + xScale + " yScale=" + yScale + " degrees=" + degrees);
		
		
		final float centerX = width / 2;
		final float centerY = height / 2;
		final Camera camera = mCamera;

		final Matrix matrix = t.getMatrix();

//		camera.save();

		matrix.setScale(xScale, yScale, 0, height);
		matrix.setRotate(degrees);
//		matrix.setScale(xScale, yScale);
		
//		camera.rotateZ(degrees);

//		camera.getMatrix(matrix);
//		camera.restore();
//
//		matrix.preTranslate(-centerX, -centerY);
//		matrix.postTranslate(centerX, centerY);
	}
}
